package com.test.gae;

import java.io.IOException;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ProtocolParser {

	private static final Logger log = Logger.getLogger(ProtocolParser.class
			.getName());

	public String handle(String inputXML) {

		Document document = null;

		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder docb = dbf.newDocumentBuilder();
			document = docb.parse(new InputSource(new java.io.StringReader(
					inputXML)));

			log.info("Parsing successfull");
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Element root = document.getDocumentElement();
		String type = root.getAttribute("type");
		if (type.equals("register")) {
			String username = root.getAttribute("username");
			String password = root.getAttribute("password");

			log.info("Saving in DATABASE new user: " + username
					+ " with password: " + password);
			DBManager manager = new DBManager();
			String result = manager.saveUser(username, password);
			if (result.equals("ok")) {
				return "<response type=\"register\" accepted=\"true\"></response>";
			} else {
				return "<response type=\"register\" accepted=\"false\"></response>";
			}

		} else if (type.equals("episode_watched")){
			String username = root.getAttribute("username");
			long episodeID = new Long(root.getAttribute("episode_id"));
			log.info("Saving in DATABASE watched episode: "+episodeID
					+ " by user: " + username);
			DBManager manager = new DBManager();
			String result = manager.saveUserEpisode(username, episodeID);
			if (result.equals("ok")) {
				return "<response type=\"episode_watched\" episode_id="+episodeID+" accepted=\"true\"></response>";
			} else {
				return "<response type=\"episode_watched\" episode_id="+episodeID+" accepted=\"false\"></response>";
			}
		} else if (type.equals("film_watched")){
			String username = root.getAttribute("username");
			long filmID = new Long(root.getAttribute("film_id"));
			log.info("Saving in DATABASE watched film: "+filmID
					+ " by user: " + username);
			DBManager manager = new DBManager();
			String result = manager.saveUserFilm(username, filmID);
			if (result.equals("ok")) {
				return "<response type=\"film_watched\" film_id="+filmID+" accepted=\"true\"></response>";
			} else {
				return "<response type=\"film_watched\" film_id="+filmID+" accepted=\"false\"></response>";
			}
		}
		/*
		 * NodeList nl = root.getElementsByTagName("ID"); if (nl.getLength() ==
		 * 0) { System.out .println(
		 * "UserDataBase: adding new article to stats, there are no articles in the file yet"
		 * ); root.appendChild(createNode(document, "ARTICLE", "ID",
		 * a.getURL()));
		 * 
		 * } else { int i = 0; for (; i < nl.getLength(); i++) { if
		 * (nl.item(i).getTextContent().equals(a.getURL())) { System.out
		 * .println("UserDataBase: this article already exists"); Node rankNode
		 * = nl.item(i).getParentNode().getChildNodes() .item(1); System.out
		 * .println("UserDataBase: adding rank to existing article");
		 * rankNode.setTextContent(((new Integer(rankNode
		 * .getTextContent())).intValue() + 10) + ""); break; } } if (i ==
		 * nl.getLength()) { System.out
		 * .println("UserDataBase: this article doesn't exist, adding new node"
		 * ); root.appendChild(createNode(document, "ARTICLE", "ID",
		 * a.getURL())); } }
		 */
		return "fuck you";

	}

}
